var usersForm = {

	/**
	 * 계정 등록/수정 FORM 출력
	 */
	editFormCall : function(callType){
		var userId = $(this).attr("userId");
		if(userId){
			userArea.editMyInfo(userId);
		}else {
			$("#dialog-userEditForm")
				.find(":input").val("").end()
				.find("select option:eq(0)").prop("selected", true).end()
				.find(".user_state").hide().end()
				.find(".newPwd").hide().end().find(".nowPwd").show().end()
				.dialog({ buttons: {
					"등록": function() {
						userArea.dataEdit("등록하시겠습니까?", usersForm.dataInsert, true);
			        },
			        "취소": function() {
			          $( this ).dialog( "close" );
			        }
				}}).dialog( "open" );

		}
		return false;
	},

	/**
	 * 계정 등록 수행
	 */
	dataInsert : function (){
		var $editForm = $("form", "#dialog-userEditForm");

		$.post(contextPath+"/user/addAJax.json", $editForm.serialize(), function(data) {

		}, "json").done(function(data) {
			if(data.code==0){

				var $usersFormDiv = $("#usersFormDiv");
				var userInfo = data.userInfo;
				var $searchDataList = $usersFormDiv.find(".searchDataList > tbody");
				var userId = common.isEmpty(userInfo.userId);
				$("<tr/>")
					.append($("<td/>").addClass("textC").append($("<span/>")))
					.append($("<td/>").append($("<a/>").addClass("userEditForm").attr("userId", userId).text(common.isEmpty(userInfo.userName))))
					.append($("<td/>").text(common.isEmpty(userId)))
					.append($("<td/>").addClass("userRoleTxt").text(common.isEmpty(userInfo.userRoleTxt)))
					.append($("<td/>").addClass("userStateTxt").text(common.isEmpty(userInfo.userStateTxt)))
					.append($("<td/>").addClass("textC").text(common.isEmptyDate(userInfo.created)))
					.append($("<td/>").addClass("textC").text(common.isEmptyDate(userInfo.lastModified))).prependTo($searchDataList);

				var pageEndNumBase = $usersFormDiv.find("[name=cutNum] option:selected").val();
				$searchDataList.find("tr").each(function(i){
					if(i==pageEndNumBase){
						$(this).remove();
					}else{
						$(this).find("td:eq(0) > span").text(++i);
					}
				});

				//페이징
				var total = parseInt($usersFormDiv.find(".pageDataTotal > strong").text(), 10)+1;
				$usersFormDiv.find(".pageDataTotal > strong").text(total);
				common.insertToPaging($usersFormDiv.find(".paginate"), total, pageEndNumBase);

				$("#dialog-userEditForm").dialog( "close" );
				common.alert("등록되었습니다");
			}else if(data.message){
				common.alert(data.message);
			}else{
				common.alert(common.serverError);
			}
		}).fail(function(data) {
			common.alert(common.serverError);
			return false;
		});
		return false;

	}
};

$(function(){
	
	$("#usersFormDiv").on("click",".userEditForm",usersForm.editFormCall);
});